package com.mktech.taraservicelib.impl;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;

import org.apache.http.client.utils.URLEncodedUtils;

import android.content.Context;
import android.os.RemoteException;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.RequestFuture;
import com.android.volley.toolbox.StringRequest;
import com.mktech.taraservicelib.entity.homefetch.HomeFetchBean;
import com.mktech.taraservicelib.entity.homefetch.HomeFetchRequestVo;
import com.mktech.taraservicelib.entity.homefetch.MovieItemVo;
import com.mktech.taraservicelib.entity.login.LoginBean;
import com.mktech.taraservicelib.entity.login.LoginRequstBean;
import com.mktech.taraservicelib.http.HttpBuilder;
import com.mktech.taraservicelib.http.VolleyHelper;
import com.mktech.taraservicelib.http.customRequest.HomeFetchBeanRequest;
import com.mktech.taraservicelib.http.customRequest.LoginBeanRequest;
import com.mktech.taraservicelib.util.ConstructParamsUtil;
import com.mktech.taraservicelib.util.LogTool;

public class TaraManager implements TaraServiceInterface
{
	private Context mContext;
	private static TaraManager mInstance;
	
	public static TaraManager obtain(Context context)
	{
		if(mInstance == null)
		{
			mInstance = new TaraManager(context);
		}
		return mInstance;
	}
	
	private TaraManager(Context context)
	{
		mContext = context.getApplicationContext();
	}
	
	@Override
	public LoginBean Login(LoginRequstBean requestBean)
	{
		// TODO Auto-generated method stub
		String url = HttpBuilder.append(HttpBuilder.USER_LOGIN);

		LogTool.i("Login url = " + url);
		RequestFuture<LoginBean> future = RequestFuture.newFuture();
		LoginBeanRequest request = new LoginBeanRequest(Request.Method.POST, url, future, future);
		request.setPostParams(ConstructParamsUtil.constructLoginParmas(requestBean));
		VolleyHelper.obtain(mContext).getRequestQueue().add(request);
		try
		{
			LoginBean loginBean = future.get();
			if (loginBean != null)
			{
				return loginBean;
			}
		}
		catch (InterruptedException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (ExecutionException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public HomeFetchBean fetchHome(HomeFetchRequestVo requestVo)
	{
		// TODO Auto-generated method stub
		String url = HttpBuilder.append(HttpBuilder.HOME_FETCH);

		StringBuilder builder = new StringBuilder(url);
		HttpBuilder.appendApi(builder, ConstructParamsUtil.constructHomeFetchParmas(requestVo));
		url = builder.toString();
		RequestFuture<HomeFetchBean> future = RequestFuture.newFuture();
		url = url.replace(" ", "%20");
		LogTool.i("fetchHome url = " + url);
		HomeFetchBeanRequest request = new HomeFetchBeanRequest(url, future, future);
		VolleyHelper.obtain(mContext).getRequestQueue().add(request);

		try
		{
			HomeFetchBean bean = future.get();
			 if(bean != null)
			 {
				 LogTool.i("fetchHome success!");
				 return bean;
			 }
		}
		catch (InterruptedException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (ExecutionException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
